package com.lancoo.answer.view.activity

import android.content.Context
import android.content.Intent
import android.graphics.Typeface
import android.view.View
import android.view.animation.DecelerateInterpolator
import androidx.core.view.marginTop
import com.drake.brv.utils.linear
import com.drake.brv.utils.setup
import com.lancoo.answer.R
import com.lancoo.answer.databinding.EvActivityCompositionCommentBinding
import com.lancoo.answer.databinding.EvItemSentenceSubBinding
import com.lancoo.answer.databinding.EvItemSentenceTitleBinding
import com.lancoo.answer.model.bean.Comment
import com.lancoo.answer.model.bean.CompositionComment
import com.lancoo.answer.model.bean.Sentence
import com.lancoo.answer.util.HtmlUtils
import com.lancoo.answer.view.adapter.SentenceListNodeAdapter
import com.lancoo.answer.view.adapter.WrapContentLinearLayoutManager
import com.lancoo.answer.view.base.BaseVMActivity
import java.math.BigDecimal

/**
 * Created by wealive on 2022/5/19.
 *
 */
class CompositionCommentActivity :
    BaseVMActivity<EvActivityCompositionCommentBinding>() {
    private val data by lazy { Companion.data!! }
    private val type by lazy { Companion.type } //显示得分or得分率 0:得分率 1:得分
    private val commentType by lazy { Companion.commentType }   //总评类型 0-作文题，1-非作文题
    private lateinit var adapter: SentenceListNodeAdapter
    private val sentences = mutableListOf<Sentence>()

    companion object {
        var data: CompositionComment? = null
        var type = 0
        var commentType = 0
        fun launch(context: Context, data: CompositionComment, type: Int, commentType: Int) {
            this.data = data
            this.commentType = commentType
            this.type = type
            context.startActivity(Intent(context, CompositionCommentActivity::class.java))
        }
    }

    override fun setLayoutID(): Int {
        return R.layout.ev_activity_composition_comment
    }

    override fun initView() {
        mBinding.rv.layoutManager = WrapContentLinearLayoutManager(this)
        //modify by dadong
        //兼容翻译题的评语详情
        if (commentType == 1) {
            mBinding.groupDetail.visibility = View.GONE
            mBinding.tvComment2.visibility = View.VISIBLE
        } else {
            mBinding.groupDetail.visibility = View.VISIBLE
            mBinding.tvComment2.visibility = View.GONE
        }
        //评语内容
        val comment = StringBuilder()
        comment.append("<font color='#ff6600'>【评语】</font>${data.Comment.ifEmpty { "暂无" }}")
        mBinding.tvComment.text = HtmlUtils.setText(comment.toString())
        mBinding.tvComment2.text = HtmlUtils.setText(comment.toString())
        //评语列表
        sentences.clear()
        sentences.addAll(data.DetailComment.Sentences.apply {
            this.forEachIndexed { index, sentence ->
                sentence.Index = index
                sentence.itemExpand = index == 0
                if (sentence.Comment!!.isEmpty()) {
                    val comment = Comment("暂无点评", "", "great")
                    sentence.Comment!!.add(comment)
                }
            }
        })
        if (sentences.isNullOrEmpty()) {
            mBinding.emptyView.visibility = View.VISIBLE
            val typeFace = Typeface.createFromAsset(getAssets(), "happyZCool.ttf")
            mBinding.tvEmpty.setTypeface(typeFace)
        } else {
            mBinding.emptyView.visibility = View.GONE
        }
        mBinding.rv.linear().setup {
            addType<Sentence>(R.layout.ev_item_sentence_title)
            addType<Comment>(R.layout.ev_item_sentence_sub)
            R.id.cl.onClick { expandOrCollapse() }
            onBind {
                when (itemViewType) {
                    R.layout.ev_item_sentence_title -> {
                        val binding = EvItemSentenceTitleBinding.bind(itemView)
                        val sentence = _data as Sentence
                        binding.tvSentence.text = "${sentence.Index + 1}.${sentence.SntOri}"
                        if (sentence.itemExpand) {
                            binding.vLine.visibility = View.VISIBLE
                            binding.ivArrow.setImageResource(R.mipmap.ev_icon_arrow_black_up)
                        } else {
                            binding.vLine.visibility = View.GONE
                            binding.ivArrow.setImageResource(R.mipmap.ev_icon_arrow_black_down)
                        }
                    }

                    R.layout.ev_item_sentence_sub -> {
                        val binding = EvItemSentenceSubBinding.bind(itemView)
                        val subSentence = _data as Comment
                        when (subSentence.Type) {
                            "warn" -> {
                                binding.ivLogo.setImageResource(R.mipmap.ev_icon_warns)
                            }

                            "error" -> {
                                binding.ivLogo.setImageResource(R.mipmap.ev_icon_wrongs)
                            }

                            "error_trp" -> {
                                binding.ivLogo.setImageResource(R.mipmap.ev_icon_tips)
                            }

                            "great" -> {
                                binding.ivLogo.setImageResource(R.mipmap.ev_icon_recommended)
                            }
                            else -> {
                                binding.ivLogo.setImageResource(R.mipmap.ev_icon_wrongs)
                            }
                        }
                        val comment = StringBuilder()
                        val msg = subSentence.Msg.replace("<b>", "<font color=\"#0099ff\">")
                            .replace("</b>", "</font>")
                        comment.append("<b>[${subSentence.Cat}]</b>$msg")
                        binding.tvComment.text = HtmlUtils.setText(comment.toString())
                    }
                }
            }
        }.models = sentences

        mBinding.sbtnBack.setOnClickListener { finish() }
        init()
    }


    fun init() {
        //此处绘制圆圈圈
        mBinding.tvScore.typeface =
            Typeface.createFromAsset(assets, "fonts/DIN Condensed Bold.ttf")
        if (type == 0) {
            mBinding.tvScore.text =
                BigDecimal(100 * data.EvalScore / data.FullScore).setScale(
                    0,
                    BigDecimal.ROUND_HALF_UP
                ).stripTrailingZeros().toPlainString()
            mBinding.tvUnit.text = "%"
            mBinding.tvFullSore.text = "得分率"
        } else {
            mBinding.tvScore.text = BigDecimal(data.EvalScore).setScale(1, BigDecimal.ROUND_HALF_UP)
                .stripTrailingZeros().toPlainString()
            mBinding.tvUnit.text = "分"
            mBinding.tvFullSore.text = "满分:${data.FullScore.toInt()}分"
        }
        mBinding.cb.setPercentData(
            100 * data.EvalScore.toFloat() / data.FullScore.toFloat(),
            "%",
            DecelerateInterpolator()
        )
        //此处绘制圈圈右边的详情
        if (data.DetailComment.ScoreCats== null){
            return
        }
        mBinding.pbVocab.progress =
            if (data.DetailComment.ScoreCats.isEmpty()) 0 else
                (100 * data.DetailComment.ScoreCats[0].ScoreRate).toInt()
        mBinding.pbSentence.progress =
            if (data.DetailComment.ScoreCats.isEmpty()) 0 else
                (100 * data.DetailComment.ScoreCats[1].ScoreRate).toInt()
        mBinding.pbSections.progress =
            if (data.DetailComment.ScoreCats.isEmpty()) 0 else
                (100 * data.DetailComment.ScoreCats[2].ScoreRate).toInt()
        mBinding.pbAbout.progress =
            if (data.DetailComment.ScoreCats.isEmpty()) 0 else
                (100 * data.DetailComment.ScoreCats[3].ScoreRate).toInt()
        mBinding.tvScoreVocab.typeface =
            Typeface.createFromAsset(assets, "fonts/DIN Condensed Bold.ttf")
        mBinding.tvScoreSentence.typeface =
            Typeface.createFromAsset(assets, "fonts/DIN Condensed Bold.ttf")
        mBinding.tvScoreSections.typeface =
            Typeface.createFromAsset(assets, "fonts/DIN Condensed Bold.ttf")
        mBinding.tvScoreAbout.typeface =
            Typeface.createFromAsset(assets, "fonts/DIN Condensed Bold.ttf")
        mBinding.tvScoreVocab.text = if (data.DetailComment.ScoreCats.isEmpty()) "0" else
            BigDecimal(100 * data.DetailComment.ScoreCats[0].ScoreRate).setScale(
                0,
                BigDecimal.ROUND_HALF_UP
            )
                .toString()
        mBinding.tvScoreSentence.text =
            if (data.DetailComment.ScoreCats.isEmpty()) "0" else
                BigDecimal(100 * data.DetailComment.ScoreCats[1].ScoreRate).setScale(
                    0,
                    BigDecimal.ROUND_HALF_UP
                )
                    .toString()
        mBinding.tvScoreSections.text =
            if (data.DetailComment.ScoreCats.isEmpty()) "0" else
                BigDecimal(100 * data.DetailComment.ScoreCats[2].ScoreRate).setScale(
                    0,
                    BigDecimal.ROUND_HALF_UP
                )
                    .toString()
        mBinding.tvScoreAbout.text =
            if (data.DetailComment.ScoreCats.isEmpty()) "0" else
                BigDecimal(100 * data.DetailComment.ScoreCats[3].ScoreRate).setScale(
                    0,
                    BigDecimal.ROUND_HALF_UP
                )
                    .toString()
//        adapter.setNewInstance(sentences)
    }
}



